package com.example.neteasecloudmusic.ui.song_list_detail;

import android.app.Dialog;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.neteasecloudmusic.R;
import com.example.neteasecloudmusic.bean.SongDetailInfo;
import com.example.neteasecloudmusic.bean.playListBean.Tracks;
import com.example.neteasecloudmusic.greenDaoGen.SongDetailController;
import com.example.neteasecloudmusic.help.DestroyActivityUtil;
import com.example.neteasecloudmusic.ui.PlayMusicActivity;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MyDialogFragment extends BottomSheetDialogFragment implements View.OnClickListener, RvUpAdapter.OnItemClickListener {
    private Integer position;
    private List<SongDetailInfo> songDetailInfos;
    private SongDetailController mSongDetailController;
    private TextView tv_song_num;
    private RecyclerView recyclerView;
    private SongDetailInfo songDetailInfo;

    public MyDialogFragment(List<SongDetailInfo> songDetailInfos, int position, SongDetailInfo songDetailInfo) {
        this.songDetailInfos = songDetailInfos;
        this.position = position;
        this.songDetailInfo = songDetailInfo;
    }

    @Override
    public void onStart() {
        super.onStart();
        BottomSheetDialog dialog = (BottomSheetDialog) getDialog();
        dialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundDrawable
                (new ColorDrawable
                        (Color.TRANSPARENT));
        FrameLayout bottom_sheet = dialog.getDelegate().findViewById(com.google.android.material.R.id.design_bottom_sheet);
        Resources resources = getResources();
        if (bottom_sheet != null) {
            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottom_sheet.getLayoutParams();
            // set max height of up windows
            layoutParams.height = getPeekHeight();
            layoutParams.setMargins(resources.getDimensionPixelSize(R.dimen.bottom_sheet_margin_left), 0,
                    resources.getDimensionPixelSize(R.dimen.bottom_sheet_margin_right), 0);
            bottom_sheet.setLayoutParams(layoutParams);
            BottomSheetBehavior<FrameLayout> behavior = BottomSheetBehavior.from(bottom_sheet);
            // peekHeight is highest
            behavior.setPeekHeight(getPeekHeight());
            // init it to be open state
            behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setStyle(STYLE_NORMAL, R.style.BottomSheetDialog);
        View view = inflater.inflate(R.layout.dialog_bottom_sheet, container, false);
        view.setBackgroundResource(R.drawable.shape_sheet_dialog_bg);
        mSongDetailController = SongDetailController.getInstance(getContext());

        recyclerView = view.findViewById(R.id.rv_up);
        tv_song_num = view.findViewById(R.id.tv_song_num);

        tv_song_num.setText("全部歌曲(" + songDetailInfos.size() + ")");
        RvUpAdapter rvUpAdapter = new RvUpAdapter(getSongListInfo(), position, recyclerView);

        rvUpAdapter.setOnItemClickListener(this);
//        rvUpAdapter.notifyItemRangeRemoved(0, songDetailInfos.size());

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(rvUpAdapter);
        // 定位当前
        recyclerView.scrollToPosition(songDetailInfos.indexOf(songDetailInfo));
        return view;
    }

    public List<SongDetailInfo> getSongListInfo () {
        return songDetailInfos;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        return new BottomSheetDialog(this.getContext());
    }

    private int getPeekHeight() {
        int peekHeight = getResources().getDisplayMetrics().heightPixels;
        return peekHeight - 2 * peekHeight / 3;
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onItemClick(int position) {
        DestroyActivityUtil.destroyActivity("PlayMusicActivity");
        Intent intent = new Intent(getContext(), PlayMusicActivity.class);
        intent.putExtra("position",position);
        startActivity(intent);
    }

    @Override
    public void onRemoveSongClick(SongDetailInfo removeInfo) {
        songDetailInfos.remove(removeInfo);
        mSongDetailController.deleteById(removeInfo.getId());
        List<SongDetailInfo> songDetailInfos = mSongDetailController.searchAll();
        tv_song_num.setText("全部歌曲(" + this.songDetailInfos.size() + ")");
        int index = this.songDetailInfos.indexOf(songDetailInfo);
//        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
//        layoutManager.scrollToPosition(index);
//        recyclerView.smoothScrollToPosition(index);
    }

}
































